/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.integration;

import java.lang.reflect.Method;
import java.util.Set;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.utils.ClassUtil;
import com.sinodata.bsm.common.vo.Event;

/**
 * <p>
 * Description: 所有集成的
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-6-26 上午10:30:36          wangyazhou        1.0         To create
 * </p>
 * 
 * @since  
 * @see     
 */
public class IntegEventDispatch {

    private static final Logger logger = Logger.getLogger(IntegEventDispatch.class);

    static Set<Class<?>> classes = null;

    static Object obj = new Object();

    public static void dispatchEvent(Event event) {
        try {
            if (classes == null) {
                synchronized (obj) {
                    if (classes == null) {
                        classes = ClassUtil.getImplClassByPath(IntegEventIf.class);
                    }
                }
            }
            for (Class<?> cls : classes) {
                try {
                    String serviceId = cls.getSimpleName();
                    Object service = SpringContextHolder.getBean(serviceId.substring(0, 1).toLowerCase() + serviceId.substring(1));
                    Method method = cls.getDeclaredMethod("execute", Event.class);
                    method.invoke(service, event);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
        } catch (Exception e) {
            logger.error("[ERROR]\t获取反实现类失败", e);
        }
    }

    public static void main(String[] args) {
        Event e = new Event();
        dispatchEvent(e);
    }

}
